package timetracker.client.forms;

import timetracker.client.ContragentService;
import timetracker.client.ContragentServiceAsync;
import timetracker.client.database.Task;
import timetracker.client.database.Contragent;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class TaskForm extends DialogBox {

	private static TaskFormUiBinder uiBinder = GWT
			.create(TaskFormUiBinder.class);

	@UiField TextBox tbContragent;
	@UiField Button btnSelectContragent;
	@UiField Button btnClearContragent;
	@UiField TextBox tbName;
	@UiField Button btnOK;
	@UiField Button btnCancel;
	
	interface TaskFormUiBinder extends UiBinder<Widget, TaskForm> {
	}
	
	private Contragent contragent = new Contragent();
	private Task task = new Task();
	
	private final ContragentServiceAsync rpcService = GWT.create(ContragentService.class);
	AsyncCallback<Task> callbackUpdateTask = new AsyncCallback<Task>() {

		@Override
		public void onFailure(Throwable caught) {
			String details = caught.getMessage();
			Window.alert(details);
		}

		@Override
		public void onSuccess(Task retValue) {
			task = retValue;
		}
	};
	AsyncCallback<Contragent> callbackUpdateContragent = new AsyncCallback<Contragent>() {

		@Override
		public void onFailure(Throwable caught) {
			String details = caught.getMessage();
			Window.alert(details);
		}

		@Override
		public void onSuccess(Contragent retValue) {
			contragent = retValue;
			onUpdateContragent();
		}
	};
	
	public void loadContragent(){
		rpcService.getContragentByKey(task.getContragentKey(), callbackUpdateContragent);
	}
	
	public void onUpdateContragent(){
		UpdateContragentDescription();
	}
	
	public TaskForm() {
		setWidget(uiBinder.createAndBindUi(this));
	}

	protected void onLoad(){
		loadContragent();
		tbName.setText(task.getName());
	}

	void UpdateContragentDescription(){
		if (contragent != null)
			tbContragent.setText(contragent.getName());
		else
			tbContragent.setText("");
	}
	
	@UiHandler("btnSelectContragent")
	void onBtnSelectContragent(ClickEvent event) {
		ContragentList dialog = new ContragentList();
		dialog.center();
		dialog.setChosenValue(contragent);
		
		dialog.addCloseHandler(new CloseHandler<PopupPanel>(){
			@Override
			public void onClose(CloseEvent<PopupPanel> event) {
				ContragentList dialog = (ContragentList) event.getSource();
				if (dialog.valueIsChosen()){
					contragent = dialog.getChosenValue();
					onUpdateContragent();
				}
			}
		});
	}
	@UiHandler("btnClearContragent")
	void onBtnClearContragent(ClickEvent event) {
		contragent = new Contragent();
		onUpdateContragent();
	}
	@UiHandler("btnOK")
	void onBtnOKClick(ClickEvent event) {
		task.setName(tbName.getText());
		task.setContragent(contragent);
		rpcService.taskSave(task, callbackUpdateTask);
		hide();
	}
	@UiHandler("btnCancel")
	void onBtnCancelClick(ClickEvent event) {
		hide();
	}

}
